package com.castle.fortress.admin.member.realinfo.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.member.realinfo.entity.MemberRealinfoEntity;
import com.castle.fortress.admin.member.realinfo.dto.MemberRealinfoDto;
import com.castle.fortress.admin.system.entity.SysUser;
import com.castle.fortress.admin.member.realinfo.service.MemberRealinfoService;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.admin.utils.WebUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 会员真实信息表 api 控制器
 *
 * @author 
 * @since 2025-04-15
 */
@Api(tags="会员真实信息表api管理控制器")
@RestController
@RequestMapping("/api/member/realinfo/memberRealinfo")
public class ApiMemberRealinfoController {
    @Autowired
    private MemberRealinfoService memberRealinfoService;


    /**
     * 会员真实信息表的分页展示
     * @param memberRealinfoDto 会员真实信息表实体类
     * @param currentPage 当前页
     * @param size  每页记录数
     * @return
     */
    @ApiOperation("会员真实信息表分页展示")
    @GetMapping("/page")
    public RespBody<IPage<MemberRealinfoDto>> pageMemberRealinfo(MemberRealinfoDto memberRealinfoDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
        Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<MemberRealinfoDto> page = new Page(pageIndex, pageSize);

        IPage<MemberRealinfoDto> pages = memberRealinfoService.pageMemberRealinfo(page, memberRealinfoDto);
        return RespBody.data(pages);
    }

    /**
     * 会员真实信息表保存
     * @param memberRealinfoDto 会员真实信息表实体类
     * @return
     */
    @ApiOperation("会员真实信息表保存")
    @PostMapping("/save")
    public RespBody<String> saveMemberRealinfo(@RequestBody MemberRealinfoDto memberRealinfoDto){
        if(memberRealinfoDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            MemberRealinfoEntity memberRealinfoEntity = ConvertUtil.transformObj(memberRealinfoDto,MemberRealinfoEntity.class);
        if(memberRealinfoService.save(memberRealinfoEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员真实信息表编辑
     * @param memberRealinfoDto 会员真实信息表实体类
     * @return
     */
    @ApiOperation("会员真实信息表编辑")
    @PostMapping("/edit")
    public RespBody<String> updateMemberRealinfo(@RequestBody MemberRealinfoDto memberRealinfoDto){
        if(memberRealinfoDto == null || memberRealinfoDto.getId() == null || memberRealinfoDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            MemberRealinfoEntity memberRealinfoEntity = ConvertUtil.transformObj(memberRealinfoDto,MemberRealinfoEntity.class);
        if(memberRealinfoService.updateById(memberRealinfoEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员真实信息表删除
     * @param ids 会员真实信息表id集合
     * @return
     */
    @ApiOperation("会员真实信息表删除")
    @PostMapping("/delete")
    public RespBody<String> deleteMemberRealinfo(@RequestBody List<Long> ids){
        if(ids == null || ids.isEmpty()){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(memberRealinfoService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 会员真实信息表详情
     * @param id 会员真实信息表id
     * @return
     */
    @ApiOperation("会员真实信息表详情")
    @GetMapping("/info")
    public RespBody<MemberRealinfoDto> infoMemberRealinfo(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            MemberRealinfoEntity memberRealinfoEntity = memberRealinfoService.getById(id);
            MemberRealinfoDto memberRealinfoDto = ConvertUtil.transformObj(memberRealinfoEntity,MemberRealinfoDto.class);
        return RespBody.data(memberRealinfoDto);
    }


}
